package org.onionframework.persistence.dao.escape;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.hibernate.dialect.HSQLDialect;
import org.hibernate.dialect.MySQL5InnoDBDialect;
import org.hibernate.dialect.Oracle10gDialect;
import org.onionframework.core.config.SystemConfig;
import org.onionframework.persistence.dao.escape.DatabaseEscape;
import org.onionframework.persistence.dao.escape.HSQLEscape;
import org.onionframework.persistence.dao.escape.MySQLEscape;
import org.onionframework.persistence.dao.escape.OracleEscape;
import org.onionframework.persistence.dao.expression.EscapedLikeExpression;

public class DatabaseEscapeUtils {

	public static final char escape = 7;
	private static Map<String,DatabaseEscape> map = new HashMap<String,DatabaseEscape>();

	static{
		map.put(Oracle10gDialect.class.getName(), new OracleEscape());
		map.put(HSQLDialect.class.getName(), new HSQLEscape());
		map.put(MySQL5InnoDBDialect.class.getName(), new MySQLEscape());
	}
	
	public static String escape(String value){
		if(StringUtils.isNotBlank(value)){
			return map.get(EscapedLikeExpression.DIALECT_HIBERNATE).escape(value);
		}
		
		return value;
	}
	
	public static String encode(String value){
		if(StringUtils.isNotBlank(value)){
			return map.get(SystemConfig.DIALECT_HIBERNATE).encode(value);
		}
		
		return value;
	}
}
